<style>
    .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {height:34px;line-height:34px;padding:0 8px;}
</style>
<body>
<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <div >
            <table id="table" class="layui-table" lay-filter="table"></table>
        </div>
    </div>
</div>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="all">全部</button>
        <a class="layui-btn layui-btn-normal" href="javascript:;" layuimini-href="product/create">添加产品</a>
    </div>
</script>
<!-- 分类列 -->
<script type="text/html" id="category_name">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="category">@{{ d.category_name }}</a>
</script>
<!-- 状态列 -->
<script type="text/html" id="is_show">
    <input type="checkbox" name="is_show" value="@{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="is_show" @{{ d.is_show == 1 ? 'checked' : '' }}>
</script>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" layuimini-href="product/@{{ d.id }}/edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    layui.use(['table', 'miniIndex'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var miniIndex = layui.miniIndex;

        // 渲染表格
        layer.load(2);
        var productList = table.render({
            elem: '#table',
            url: 'product/list',
            toolbar: '#toolbarDemo',
            page: true,
            limit: 10,
            cols: [[
                {type: 'numbers'},
                {field: 'id', title: '产品ID'},
                {field: 'product_name', title: '产品名称'},
                {field: 'category_name', title: '分类名称', templet: '#category_name',align: 'center'},
                {title: '审核', templet: '#is_show',align:'center'},
                {field: 'order', title: '排序', align:'center'},
                {field: 'created_at', title: '添加时间'},
                {templet: '#auth-state', width: 120, align: 'center', title: '操作'}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });

        // 切换推荐状态
        miniIndex.tableSwitch(productList, 'products' , 'is_show');
        //监听工具条 // 表格操作事件
        table.on('tool(table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            // 选取分类下产品
            if (layEvent === 'category') {
                productList.reload({
                    where:{category_id: data.category_id}
                })
            }
            if (layEvent === 'del'){
                layer.confirm('真的删除这个吗', function (index) {
                    layer.load(2);
                    $.ajax({
                        url: url + '/' + data.id,
                        method: 'DELETE',
                        success: function (res) {
                            layer.msg(res.msg,function () {
                                obj.del();
                                layer.closeAll('loading');
                                layer.close(index);
                            })
                        }
                    })
                });
            }
        });
        /**
         * toolbar事件监听
         */
        table.on('toolbar(table)', function (obj) {
            if (obj.event === 'all') {   // 监听添加操作
                productList.reload({
                    where:{category_id: 0}
                })
            }
        });
    });
</script>
</body>
